package com.jlastu.dlxy_androidapp.adapter;

import android.content.Context;
import android.content.DialogInterface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import androidx.recyclerview.widget.RecyclerView;

import com.jlastu.dlxy_androidapp.R;
import com.jlastu.dlxy_androidapp.entity.Address;

import java.util.List;

public class AddressAdapter extends RecyclerView.Adapter<AddressAdapter.AddressViewHolder> {

    private List<Address> addressList;
    private Context context;

    public AddressAdapter(Context context, List<Address> addressList) {
        this.context = context;
        this.addressList = addressList;
    }

    @NonNull
    @Override
    public AddressViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.address_list_item, parent, false);
        return new AddressViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull AddressViewHolder holder, int position) {
        Address address = addressList.get(position);
        holder.name.setText("收件人 ：" +address.getName());
        holder.phone.setText("手机号  ：" + address.getPhone());
        holder.address.setText("详细地址 ：" + address.getAddress());

        //删除操作
        holder.deleteButton.setOnClickListener(v -> {
            AlertDialog.Builder builder = new AlertDialog.Builder(context);
            builder.setTitle("确认删除")
                    .setMessage("确定要删除此项吗？")
                    .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            addressList.remove(position);
                            notifyItemRemoved(position);
                            notifyItemRangeChanged(position, addressList.size());
                        }
                    })
                    .setNegativeButton("取消", null)
                    .show();
        });

        //修改操作
        holder.editButton.setOnClickListener(v -> {

            Address selectedAddress = addressList.get(position);

            showEditAddressDialog(selectedAddress, position);
        });
    }

    private void showEditAddressDialog(Address address, int position) {
        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        LayoutInflater inflater = LayoutInflater.from(context);
        View dialogView = inflater.inflate(R.layout.dialog_add_address, null);
        builder.setView(dialogView);

        EditText editTextName = dialogView.findViewById(R.id.editTextName);
        EditText editTextPhone = dialogView.findViewById(R.id.editTextPhone);
        EditText editTextAddress = dialogView.findViewById(R.id.editTextAddress);


        editTextName.setText(address.getName());
        editTextPhone.setText(address.getPhone());
        editTextAddress.setText(address.getAddress());

        builder.setTitle("修改地址信息")
                .setPositiveButton("保存", (dialog, id) -> {
                    String name = editTextName.getText().toString().trim();
                    String phone = editTextPhone.getText().toString().trim();
                    String newAddress = editTextAddress.getText().toString().trim();

                    if (!name.isEmpty() && !phone.isEmpty() && !newAddress.isEmpty()) {
                        // Update the address in the list
                        address.setName(name);
                        address.setPhone(phone);
                        address.setAddress(newAddress);
                        notifyItemChanged(position);
                    }
                })
                .setNegativeButton("取消", (dialog, id) -> dialog.dismiss());

        AlertDialog alertDialog = builder.create();
        alertDialog.show();

    }


    @Override
    public int getItemCount() {
        return addressList.size();
    }

    public static class AddressViewHolder extends RecyclerView.ViewHolder {
        TextView name, phone, address;
        Button deleteButton, editButton;

        public AddressViewHolder(@NonNull View itemView) {
            super(itemView);
            name = itemView.findViewById(R.id.name);
            phone = itemView.findViewById(R.id.phone);
            address = itemView.findViewById(R.id.address);
            deleteButton = itemView.findViewById(R.id.deleteButton);
            editButton = itemView.findViewById(R.id.editButton);
        }
    }
}